package com.zt.yongxinbao.service.impl;

import com.zt.yongxinbao.entity.UserDetail;
import com.zt.yongxinbao.mapper.UserDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class UserDetailServiceImpl implements com.zt.yongxinbao.service.UserDetailService {
    @Autowired
    private UserDetailMapper userDetailMapper;

    private UserDetail userDetail;
    @Override
    public UserDetail getUserDetail(String username) {
       return userDetailMapper.selectByUsername(username);
    }
    @Override
    public Boolean updateUserAvatar(String username, String avatar) {
        userDetail=userDetailMapper.selectByUsername(username);
       if (userDetail==null){
           UserDetail userDetail1=new UserDetail();
           userDetail1.setUsername(username);
           userDetail1.setAvatar(avatar);
           userDetailMapper.insert(userDetail1);
       }else{
           if (avatar.equals(userDetail.getAvatar())) return true;
       }
      if(userDetailMapper.updateUserAvatar(username,avatar)==1) return true;
      return false;
    }

    @Override
    public Boolean updateUserTelPhone(String username, String telPhone) {
        userDetail=userDetailMapper.selectByUsername(username);
        if (userDetail==null){
            UserDetail userDetail1=new UserDetail();
            userDetail1.setUsername(username);
            userDetail1.setAvatar(telPhone);
            userDetailMapper.insert(userDetail1);
        }else {
            if (telPhone.equals(userDetail.getTelPhone())) return  true;
        }
        List<UserDetail> userDetailList=userDetailMapper.selectAll();
        Iterator iterator=userDetailList.iterator();
        while (iterator.hasNext()){
            UserDetail userDetail1= (UserDetail) iterator.next();
            if (telPhone.equals(userDetail1.getTelPhone())) return false;
        }
        if(userDetailMapper.updateUserTelPhone(username,telPhone)==1) return true;
        return false;
    }

    @Override
    public Boolean updateUserActualName(String username, String actualName) {
        userDetail=userDetailMapper.selectByUsername(username);
        if (userDetail==null){
            UserDetail userDetail1=new UserDetail();
            userDetail1.setUsername(username);
            userDetail1.setAvatar(actualName);
            userDetailMapper.insert(userDetail1);
        }else {
            if (actualName.equals(userDetail.getActualName())) return true;
        }
        if(userDetailMapper.updateUserActualName(username,actualName)==1) return true;
        return false;
    }

    @Override
    public Boolean updateUserSex(String username, String sex) {
        userDetail=userDetailMapper.selectByUsername(username);
        if (userDetail==null){
            UserDetail userDetail1=new UserDetail();
            userDetail1.setAvatar(sex);
            userDetail1.setUsername(username);
            userDetailMapper.insert(userDetail1);
        }else {
            if (sex.equals(userDetail.getSex())) return true;
        }
        if(userDetailMapper.updateUserSex(username,sex)==1) return true;
        return false;
    }
}
